package com.haidong.component;

import com.haidong.SqlFunction;
import com.haidong.pojo.House;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

public class MyHousePanel extends JPanel {
    public static JPanel select(){
        JPanel panel = new JPanel(new BorderLayout());

        JPanel up = new JPanel(new GridLayout(1,9));
        JLabel l1 = new JLabel("房名:");
        JLabel l2 = new JLabel("建筑面积:");
        JLabel l3 = new JLabel("阳台面积:");
        JLabel l4 = new JLabel("房屋等级:");
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        TextField tf3 = new TextField();
        TextField tf4 = new TextField();
        JButton bt1 = new JButton("查询");

        up.add(l1);
        up.add(tf1);
        up.add(l2);
        up.add(tf2);
        up.add(l3);
        up.add(tf3);
        up.add(l4);
        up.add(tf4);
        up.add(bt1);

        JPanel center = new JPanel(new GridLayout(1,1));

        //动态显示表格内容,用defaultTable是为了保证数据刷新,用scrollPane是为了显示表头
        Object[] houseHead = new Object[]{"房子编号","房名","房主","建筑面积","阳台面积","房屋等级"};
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},houseHead);
        JTable table = new JTable(tableModel);
        table.setRowHeight(30);
        JScrollPane scrollPane = new JScrollPane(table);
        center.add(scrollPane);

        //补充监听器功能,实现按下按钮时获取数据库内容
        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                House house = new House();
                house.setName("".equals(tf1.getText()) ? null :"%"+tf1.getText()+"%");
                house.setArea("".equals(tf2.getText()) ? null :Double.valueOf(tf2.getText()));
                house.setBalconyArea("".equals(tf3.getText()) ? null :Double.valueOf(tf3.getText()));
                house.setHouseLevel("".equals(tf4.getText()) ? null :Integer.valueOf(tf4.getText()));
                List<House> houses = SqlFunction.selectHouse(house);
                Object[][] data = new Object[houses.size()][6];
                for (int i = 0; i < houses.size(); i++) {
                    data[i] = getSingleHouseData(houses.get(i));
                    tableModel.setDataVector(data,houseHead);
                }
            }
        });

        panel.add(up,BorderLayout.NORTH);
        panel.add(center,BorderLayout.CENTER);

        return panel;
    }
    public static Object[] getSingleHouseData(House house){
        String none = "暂无";
        //接受数据并过滤掉null
        return new Object[]{
                house.getId() != null ? house.getId() : none,
                house.getName() != null ? house.getName() : none,
                house.getHostId() != null ? house.getHostId() : none,
                house.getArea() != null ? house.getArea() : none,
                house.getBalconyArea() != null ? house.getBalconyArea() : none,
                house.getHouseLevel() != null ? house.getHouseLevel() : none
        };
    }

    public static JPanel add(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,3));
        JLabel[] labels = {new JLabel("请输入房名*:"),new JLabel("请输入建筑面积*:"),
                new JLabel("请输入阳台面积*:"),new JLabel("请输入当前房主id:"),
                new JLabel("请输入前房主id:"),new JLabel("请输入首任房主id:"),
                new JLabel("请输入首次交付年:"),new JLabel("请输入首次交付月:"),
                new JLabel("请输入房屋等级:")};
        TextField[] textField = new TextField[9];
        JPanel[] panels = new JPanel[9];
        for (int i = 0; i < 9; i++) {
            textField[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            labels[i].setFont(new Font("Dialog",0,20));
            textField[i].setFont(new Font("Dialog",0,25));
            panels[i].add(labels[i]);
            panels[i].add(textField[i]);
        }

        JButton button = new JButton("确认添加");
        button.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("".equals(textField[0].getText())||"".equals(textField[1].getText())||"".equals(textField[2].getText())){
                    label.setText("请输入必选项!");
                }else{
                    House house = new House();
                    house.setName("".equals(textField[0].getText()) ? null :textField[0].getText());
                    house.setArea("".equals(textField[1].getText()) ? null :Double.valueOf(textField[1].getText()));
                    house.setBalconyArea("".equals(textField[2].getText()) ? null :Double.valueOf(textField[2].getText()));
                    house.setHostId("".equals(textField[3].getText()) ? null :Integer.valueOf(textField[3].getText()));
                    house.setLastId("".equals(textField[4].getText()) ? null :Integer.valueOf(textField[4].getText()));
                    house.setFirstId("".equals(textField[5].getText()) ? null :Integer.valueOf(textField[5].getText()));
                    house.setStartYear("".equals(textField[6].getText()) ? null :Integer.valueOf(textField[6].getText()));
                    house.setStartMonth("".equals(textField[7].getText()) ? null :Integer.valueOf(textField[7].getText()));
                    house.setHouseLevel("".equals(textField[8].getText()) ? null :Integer.valueOf(textField[8].getText()));
                    SqlFunction.insertHouse(house);
                    label.setText("添加成功!");
                }
            }
        });

        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 0; i < 3; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 3; i < 6; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 6; i < 9; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(button);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}

        panel.add(upper);
        return panel;
    }

    public static JPanel del(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,1));
        JLabel label1 = new JLabel("请输入房屋id:");
        label1.setFont(new Font("Dialog",0,20));
        TextField textField1 = new TextField();
        textField1.setFont(new Font("Dialog",0,25));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        panel1.add(label1);
        panel1.add(textField1);
        JButton button1 = new JButton("确认删除");
        button1.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SqlFunction.deleteHouse("".equals(textField1.getText()) ? 0 : Integer.valueOf(textField1.getText()));
                label.setText("删除成功!");
            }
        });
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(button1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        panel.add(new JLabel(""));
        panel.add(upper);
        panel.add(new JLabel(""));
        return panel;
    }

    public static JPanel update(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,3));
        JLabel labelId = new JLabel("请输入id(此为索引):");
        labelId.setFont(new Font("dialog",0,20));
        TextField textFieldId = new TextField();
        textFieldId.setFont(new Font("dialog",0,25));
        JPanel panelId = new JPanel(new GridLayout(1,2));
        panelId.add(labelId);
        panelId.add(textFieldId);
        JLabel[] labels = {new JLabel("请输入房名:"),new JLabel("请输入建筑面积:"),
                new JLabel("请输入阳台面积:"),new JLabel("请输入当前房主id:"),
                new JLabel("请输入前房主id:"),new JLabel("请输入首任房主id:"),
                new JLabel("请输入首次交付年:"),new JLabel("请输入首次交付月:"),
                new JLabel("请输入房屋等级:")};
        TextField[] textField = new TextField[9];
        JPanel[] panels = new JPanel[9];
        for (int i = 0; i < 9; i++) {
            textField[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            labels[i].setFont(new Font("Dialog",0,20));
            textField[i].setFont(new Font("Dialog",0,25));
            panels[i].add(labels[i]);
            panels[i].add(textField[i]);
        }

        JButton button = new JButton("确认修改");
        button.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                House house = new House();
                house.setId(Integer.valueOf(textFieldId.getText()));
                house.setName("".equals(textField[0].getText()) ? null : textField[0].getText());
                house.setArea("".equals(textField[1].getText()) ? null :Double.valueOf(textField[1].getText()));
                house.setBalconyArea("".equals(textField[2].getText()) ? null :Double.valueOf(textField[2].getText()));
                house.setHostId("".equals(textField[3].getText()) ? null :Integer.valueOf(textField[3].getText()));
                house.setLastId("".equals(textField[4].getText()) ? null :Integer.valueOf(textField[4].getText()));
                house.setFirstId("".equals(textField[5].getText()) ? null :Integer.valueOf(textField[5].getText()));
                house.setStartYear("".equals(textField[6].getText()) ? null :Integer.valueOf(textField[6].getText()));
                house.setStartMonth("".equals(textField[7].getText()) ? null :Integer.valueOf(textField[7].getText()));
                house.setHouseLevel("".equals(textField[8].getText()) ? null :Integer.valueOf(textField[8].getText()));
                SqlFunction.updateHouse(house);
                label.setText("修改成功!");
            }
        });

        upper.add(new JLabel(""));
        upper.add(panelId);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 0; i < 3; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 3; i < 6; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        for (int i = 6; i < 9; i++) {upper.add(panels[i]);}
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(button);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(new JLabel(""));
        upper.add(label);
        upper.add(new JLabel(""));

        panel.add(upper);
        return panel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = MyHousePanel.select();
        frame.add(panel);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}
